/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.IO;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
 *
 * @author kylin
 */
public class FileTypes{
    public static File xmlfile=null;
    public static Document document=null;
    public static String type="";
    public static ArrayList<String> list=new ArrayList<String>();
    public static Element root=null;
    public static String name;
    
    public static void main(String args[]){
        String path="//media/kylin/Design";
        start(path);
        
        
    }
    
    public static void start(String path){
        File file=new File(path);
        File files[]=file.listFiles();
        for(int i=0;i<files.length;i++){
            File sub=files[i];
            if(sub.isDirectory()){
                //System.out.println(sub.getName()+"/");
                iterator(sub.getAbsolutePath());
            }else{
                type=sub.getName();
                if(type.contains(".")){
                    type=type.substring(type.lastIndexOf(".")+1,type.length());
                    type=type.toLowerCase();
                    //System.out.println(type);
                    if(!list.contains(type)){
                        list.add(type);
                    }
                }
                //System.out.println(sub.getName());
            }
        }
        print();
    }
    public static void iterator(String path){
        File file=new File(path);
        File files[]=file.listFiles();
        for(int i=0;i<files.length;i++){
            File sub=files[i];
            if(sub.isDirectory()){
                //System.out.println(sub.getName()+"/");
                iterator(sub.getAbsolutePath());
            }else{
                //System.out.println(sub.getName());
                type=sub.getName();
                if(type.contains(".")){
                    type=type.substring(type.lastIndexOf(".")+1,type.length());
                    type=type.toLowerCase();
                    //System.out.println(type);
                    if(!list.contains(type)){
                        list.add(type);
                    }
                }
            }
        }
    }
    
    public static void print(){
        createXML();
        
        Iterator itor=list.iterator();
        while(itor.hasNext()){
            name=itor.next().toString();
            addElement(root,name);
            //System.out.println(itor.next());
        }
        write();
    }
    public static void createXML(){
        xmlfile=new File("/home/kylin/Documents/FileTypes3.xml");
        document=DocumentHelper.createDocument();
        root=document.addElement("FileTypes");
        document.setRootElement(root);
        //write();
    }
    public static void addElement(Element father,String name){
        Element type=father.addElement("Type");
        type.setAttributeValue("name", name);
    }
    public static void write(){
        try{
            OutputFormat outFmt = new OutputFormat("\t", true);  
            outFmt.setEncoding("UTF-8");
            XMLWriter output = new XMLWriter(new FileOutputStream(xmlfile),outFmt); 
            output.write(document);
            output.close();
	}catch(IOException e){
            e.printStackTrace();
	}
    }
}
